import msgCheck from '../../util/msgCheck'
import imgCheck from '../../util/imgCheck'
import uploadImgList from '../../util/uploadImgList'
import commentApi from '../../api/commentApi'
import pointApi from '../../api/pointApi'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    imgList: [],
    fileList: [],
    deleteFiles: [],
    activeId: ''
  },

  onLoad(options) {
    this.data.activeId = options.activeId
  },

  contentInput: function (e) {
    var val = e.detail.value;

    this.setData({
      content: val
    });
  },

  previewImage(e) {
    var url = e.currentTarget.dataset.url
    wx.previewImage({
      urls: this.data.imgList,
      current: url
    })
  },

  chooseImage: function () {
    wx.chooseMedia({
      count: 9 - this.data.imgList.length,
      mediaType: ['image'],
      success: (res) => {
        let list = res.tempFiles.map(obj => obj.tempFilePath)

        const tempFilePaths = this.data.imgList.concat(list)
        this.setData({
          imgList: tempFilePaths
        })
      }
    })
  },

  delImg(e) {
    var index = e.currentTarget.dataset.index
    this.data.imgList.splice(index, 1)
    var fileIds = this.data.fileList.splice(index, 1)
    if (fileIds[0]) {
      this.data.deleteFiles.push(fileIds[0])
    }
    this.setData({
      imgList: this.data.imgList
    })
  },


  onChooseAvatar(e) {
    if (wx.getStorageSync('nickname')) {
      this.addComment()
      return
    }
    this.selectComponent('#avatarNickname').open()

  },
  avatarNicknameConfirm(e) {

    this.addComment()
  },

  async addComment() {
    if (!this.data.content && this.data.imgList.length == 0) {
      wx.showToast({
        title: '没有可提交的内容',
        icon: 'none'
      })
      return
    }

    let pass = await msgCheck(this.data.content)
    if (!pass) {
      return
    }

    pass = await imgCheck(this.data.imgList)
    if (!pass) {
      return
    }

    wx.showLoading({
      title: '正在提交评论',
      mask: true
    })
    var res = await uploadImgList(this.data.imgList, this.data.fileList)

    let params = {
      openid: wx.getStorageSync('openid'),
      avatarUrl: wx.getStorageSync('avatarUrl'),
      nickname: wx.getStorageSync('nickname'),
      content: this.data.content,
      fileList: res.map((f) => f.fileID),
      imgList: res.map((f) => f.tempFileURL),
      activeId: this.data.activeId
    }

    console.log(params)

    var data = await commentApi.add(params)
    wx.showToast({
      title: '评论成功',
    })
    pointApi.addNum({
      openid: wx.getStorageSync('openid'),
      num: 10
    })
    wx.setStorageSync('addCommentSuccess', 1)
    wx.navigateBack()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})